package com.mr.qa.dao.impl;

import java.util.HashMap;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.mr.qa.bo.UserMoneySpendHistory;
import com.mr.qa.exception.QaException;


public class UserMoneySpendHistoryDAO extends BaseHibernateDAO {
	
	private static final Log log = LogFactory.getLog(UserMoneySpendHistoryDAO.class);
	
	public void save(UserMoneySpendHistory transientInstance) {
        log.debug("saving UserMoneySpendHistory instance");
        try {
            getSession().save(transientInstance);
            log.debug("save successful");
        } catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
        }
    }
    
	public void delete(UserMoneySpendHistory persistentInstance) {
        log.debug("deleting UserMoneySpendHistory instance");
        try {
            getSession().delete(persistentInstance);
            log.debug("delete successful");
        } catch (RuntimeException re) {
            log.error("delete failed", re);
            throw re;
        }
    }
    
    public UserMoneySpendHistory findById( java.lang.Integer id) {
        log.debug("getting UserMoneySpendHistory instance with id: " + id);
        try {
        	UserMoneySpendHistory instance = (UserMoneySpendHistory) getSession()
                    .get("com.mr.qa.bo.UserMoneySpendHistory", id);
            return instance;
        } catch (RuntimeException re) {
            log.error("get failed", re);
            throw re;
        }
    }
    
    
    public List findByExample(UserMoneySpendHistory instance) {
        log.debug("finding UserMoneySpendHistory instance by example");
        try {
            List results = getSession()
                    .createCriteria("com.mr.qa.bo.UserMoneySpendHistory")
                    .add(Example.create(instance))
            .list();
            log.debug("find by example successful, result size: " + results.size());
            return results;
        } catch (RuntimeException re) {
            log.error("find by example failed", re);
            throw re;
        }
    }    
    
    public List findByProperty(String propertyName, Object value) {
      log.debug("finding UserMoneySpendHistory instance with property: " + propertyName
            + ", value: " + value);
      try {
         String queryString = "from UserMoneySpendHistory as model where model." 
         						+ propertyName + "= ?";
         Query queryObject = getSession().createQuery(queryString);
		 queryObject.setParameter(0, value);
		 return queryObject.list();
      } catch (RuntimeException re) {
         log.error("find by property name failed", re);
         throw re;
      }
	}
	
    public UserMoneySpendHistory merge(UserMoneySpendHistory detachedInstance) {
        log.debug("merging UserMoneySpendHistory instance");
        try {
            UserMoneySpendHistory result = (UserMoneySpendHistory) getSession()
                    .merge(detachedInstance);
            log.debug("merge successful");
            return result;
        } catch (RuntimeException re) {
            log.error("merge failed", re);
            throw re;
        }
    }

    public void attachDirty(UserMoneySpendHistory instance) {
        log.debug("attaching dirty UserMoneySpendHistory instance");
        try {
            getSession().saveOrUpdate(instance);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public void attachClean(UserMoneySpendHistory instance) {
        log.debug("attaching clean UserMoneySpendHistory instance");
        try {
            getSession().lock(instance, LockMode.NONE);
            log.debug("attach successful");
        } catch (RuntimeException re) {
            log.error("attach failed", re);
            throw re;
        }
    }
    
    public Integer getUserSpendAmount(Integer userId, String whereClause) {
		StringBuffer queryString = new StringBuffer("select sum(money) from UserMoneySpendHistory where user.id = ?");
		if(whereClause != null){
			queryString.append("and " + whereClause);
		}
		HashMap parameters = new HashMap();
        parameters.put(0,userId);
		return this.countQuery(queryString.toString(), parameters);
    }
    
    public Integer countUserSpend(Integer userId, String whereClause) {
		StringBuffer queryString = new StringBuffer("select count(*) from UserMoneySpendHistory as model where model.user.id = ?");
		if(whereClause != null){
			queryString.append("and " + whereClause);
		}
		HashMap parameters = new HashMap();
        parameters.put(0,userId);
		return this.countQuery(queryString.toString(), parameters);
    }
    
    public List findUserSpend(Integer userId, String whereClause) throws QaException
    {
        try
        {
            StringBuffer queryString = new StringBuffer("from UserMoneySpendHistory where user.id = ? ");
            if(whereClause != null){
            	queryString.append("and " + whereClause);
            }
            queryString.append(" order by date desc"); 
            HashMap parameters = new HashMap();
            parameters.put(0,userId);
            return this.getQueryList(queryString.toString(),parameters);            
        }
        catch(Exception e)
        {
            throw new QaException(e);
        }
    }
    
    public List getAllUserAwards() throws QaException{
    	 try
         {
             StringBuffer queryString = new StringBuffer("select sum(model.money), model.user from UserMoneySpendHistory model where money>0 group by model.user.id");
             return this.getQueryList(queryString.toString(), null);
             
         }
         catch(Exception e)
         {
             throw new QaException(e);
         }
    }
    
    public Integer countAllUserAwards() throws QaException{
   	 try
        {
            StringBuffer queryString = new StringBuffer("select sum(model.money), model.user from UserMoneySpendHistory model where money>0 group by model.user.id)");
            
            return this.getQueryList(queryString.toString(), null).size();
        }
        catch(Exception e)
        {
            throw new QaException(e);
        }
     }
    
    public List getSpend(String whereClause, String orderBy) throws QaException{
    	try{
    		StringBuffer queryString = new StringBuffer("from UserMoneySpendHistory");
    		if(whereClause != null && whereClause.length() > 0){
    			queryString.append(" where " + whereClause);
    		}
    		if(orderBy != null && orderBy.length() > 0){
    			queryString.append(" order by " + orderBy);
    		}
    		return this.getQueryList(queryString.toString(), null);
    	}catch(Exception e)
        {
            throw new QaException(e);
        }
    }
    
    public Integer getSpendCount(String whereClause, String orderBy) throws QaException{
    	try{
    		StringBuffer queryString = new StringBuffer("select count(*) from UserMoneySpendHistory");
    		if(whereClause != null && whereClause.length() > 0){
    			queryString.append(" where " + whereClause);
    		}
    		if(orderBy != null && orderBy.length() > 0){
    			queryString.append(" order by " + orderBy);
    		}
    		return this.countQuery(queryString.toString(), null);
    	}catch(Exception e)
        {
            throw new QaException(e);
        }
    }

   
}
